package biolean;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import biolean.commands.Command;

public class FileListCanvas extends JPanel{
	private AppCanvas AppPanel;
	private JList FileTypeList;
	
	public FileListCanvas(AppCanvas AppPanel) {
		this.AppPanel = AppPanel;
		
			
		String[] data = Biolean.fileRegister.getFileTypeNames();
		FileTypeList = new JList(data);
		FileTypeList.setCellRenderer(new MyCellRenderer());
		this.add(FileTypeList);
		FileTypeList.addMouseListener(new MAdapter());
	}
	
	public JList getJlist () {
		return FileTypeList;
	}
	
	public AppCanvas getAppanel () {
		return AppPanel;
	}
	

	private class MAdapter extends MouseAdapter {

		public void mouseClicked(MouseEvent e) {
	         if (e.getClickCount() == 2) {
	             //int index = FileTypeList.locationToIndex(e.getPoint());
	             int index = FileTypeList.getSelectedIndex();
	             Class<?> instanceClass = Biolean.fileRegister.getBioFileClass(index);
	             String name = Biolean.fileRegister.getTypeName(index);
	             AppPanel.AddEmptyFile(instanceClass, name);
	          }
	         //AppPanel.requestFocus();
	         
	     
	     }	
	}
	
	class MyCellRenderer extends JLabel implements ListCellRenderer, Commons {
//	     final static ImageIcon longIcon = new ImageIcon("long.gif");
//	     final static ImageIcon shortIcon = new ImageIcon("short.gif");

	     // This is the only method defined by ListCellRenderer.
	     // We just reconfigure the JLabel each time we're called.

	     public Component getListCellRendererComponent(
	       JList list,
	       Object value,            // value to display
	       int index,               // cell index
	       boolean isSelected,      // is the cell selected
	       boolean cellHasFocus)    // the list and the cell have the focus
	     {
	    	 String s = value.toString();
	         setText(s);
	         this.setPreferredSize(new Dimension(COMMAND_LABEL_WIDTH,COMMAND_LABEL_HEIGHT));
	         //setIcon((s.length() > 10) ? longIcon : shortIcon);
	   	   if (isSelected) {
	             setBackground(list.getSelectionBackground());
		       setForeground(list.getSelectionForeground());
		   }
	         else {
		       setBackground(list.getBackground());
		       setForeground(list.getForeground());
		   }
		   setEnabled(list.isEnabled());
		   setFont(list.getFont());
	       setOpaque(true);
	       return this;
	     }
	 }	
}


